//
//  DRCircleView.m
//  bse
//
//  Created by 3iRobot2020 on 2021/6/5.
//

#import "DRCircleView.h"

#define KHWCircleFont [UIFont boldSystemFontOfSize:16.0f]

@interface DRCircleView ()

@property (nonatomic, strong) UILabel *cLabel;

@end

@implementation DRCircleView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        self.lineWidth = 0;
        //百分比标签
        _cLabel = [[UILabel alloc] initWithFrame:self.bounds];
        _cLabel.font = KHWCircleFont;
        _cLabel.textColor = [UIColor clearColor];
        _cLabel.textAlignment = NSTextAlignmentCenter;
        
        [self addSubview:_cLabel];

    }
    
    return self;
}

- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    NSString *preStr =   NSLocalizedString(@"升级中", @"");
    _cLabel.text = [NSString stringWithFormat:@"%@\n%d%%",preStr, (int)floor(progress * 100)];
    _cLabel.numberOfLines = 0;
    _cLabel.textAlignment = NSTextAlignmentCenter;
    _cLabel.textColor = self.progressColor;
    [self setNeedsDisplay];
    
}

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    _cLabel.frame = self.bounds;
}

-(void)setProgressColor:(UIColor *)progressColor{
    _progressColor = progressColor;
}

-(void)setLineWidth:(CGFloat)lineWidth{
    _lineWidth = lineWidth;
}
- (void)drawRect:(CGRect)rect
{
    //路径
    UIBezierPath *path = [[UIBezierPath alloc] init];
    //线宽
    path.lineWidth = self.lineWidth;
    //颜色
    [self.progressColor set];
    //拐角
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    //半径
    CGFloat radius = (MIN(rect.size.width, rect.size.height) - self.lineWidth) * 0.5;
    //画弧（参数：中心、半径、起始角度(3点钟方向为0)、结束角度、是否顺时针）
    [path addArcWithCenter:(CGPoint){rect.size.width * 0.5, rect.size.height * 0.5} radius:radius startAngle:M_PI * 1.5 endAngle:M_PI * 1.5 + M_PI * 2 * _progress clockwise:YES];
    //连线
    [path stroke];

}

@end


